package com.weidft.web.wd.widget.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.roma.core.annotations.In;
import com.alibaba.roma.core.context.scope.ScopeType;
import com.weidft.biz.permission.UserGrant;
import com.weidft.web.biz.CacheTools;
import com.weidft.web.biz.Menu;
import com.weidft.web.biz.MenuListManger;
import com.weidft.web.wd.controller.Basic;

public class MenulistWidget extends Basic{

    @In(scope = ScopeType.APPLICATION)
    private MenuListManger menuListManger;
    
    public void execute(String moudel, Map<String, Object> model) {
        Menu menu = getMenu(moudel);
        UserGrant userGrant = null;
        if  (loginUserDO!=null)
		{
			Integer userId = loginUserDO.getId();
			if (userId!=null)
			{
				userGrant = CacheTools.getUserGrant(userId);
			}
	        model.put("currentMenu", loginUserDO.getCurrentMenu());
		}
			
        if (menu != null && menu.getList() != null && userGrant!=null) {
            List<MenuUnit> menuUnitList = new ArrayList<MenuUnit>();
            List<String> tmp = menu.getList();
            int size = tmp.size();
            for (int i = 0; i < size; i = i + 2) {
            	String name =  tmp.get(i + 1);
            	if (userGrant.checkMenu(moudel, name))
            	{
            		menuUnitList.add(new MenuUnit(tmp.get(i),name));
            	}
            }
            model.put("menuUnitList", menuUnitList);
        }
    }

    private Menu getMenu(String moduel) {
        if (CAIGOU.equals(moduel)) {
            return menuListManger.getCaigouMenu();
        }
        if (USER.equals(moduel)) {
            return menuListManger.getUserMenu();
        }
        if (QUALITY.equals(moduel)) {
            return menuListManger.getQualityMenu();
        }
        if (BASEDATA.equals(moduel)) {
            return menuListManger.getBasedataMenu();
        }
        if (CRAFT.equals(moduel)) {
            return menuListManger.getCraftMenu();
        }
        if (PLAN.equals(moduel)) {
            return menuListManger.getPlanMenu();
        }
        if (PLANTWO.equals(moduel)) {
            return menuListManger.getPlantwoMenu();
        }        
        if (INQUIRY.equals(moduel)) {
            return menuListManger.getInquiryMenu();
        }
        if (WAREHOUSE.equals(moduel)) {
            return menuListManger.getWarehouseMenu();
        }
        return null;
    }

    private final static String CAIGOU      = "caigou";
    private final static String USER        = "user";
    private final static String QUALITY     = "quality";
    private final static String BASEDATA    = "basedata";
    private final static String CRAFT       = "craft";
    private final static String PLAN		= "plan";
    private final static String PLANTWO		= "plantwo";
    private final static String INQUIRY		= "inquiry";
    private final static String WAREHOUSE		= "warehouse";

    public static class MenuUnit {

        public MenuUnit(String url, String name) {
            this.url = url;
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public String getName() {
            return name;
        }

        private String url;
        private String name;
    }
}
